﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2023-06-21 15:32
    /// 描 述：工况表
    /// </summary>
    public class ec_CaseBLL : ec_CaseIBLL
    {
        Repository<ec_CaseEntity> caseRepo = new Repository<ec_CaseEntity>();
        ec_CaseService caseServ;
        public ec_CaseBLL()
        {
            caseServ = new ec_CaseService();
        }
        #region 获取数据
        /// <summary>
        /// 获取列表 
        /// <param name="ProjId"></param>
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_CaseEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var tableName = TableName<ec_CaseEntity>(queryParam["ProjectId"]?.ToString());
                var query = Db.Queryable<ec_CaseEntity>().AS(tableName)
                    .WhereIF(queryParam["CaseName"] != null, x => x.CaseName.Contains(queryParam["CaseName"].ToString()));
                if (pagination == null)
                {
                    return query.ToList();
                }
                else
                {
                    return query.ToPageList(pagination.page, pagination.rows);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_CaseEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                var tableName = TableName<ec_CaseEntity>(ProjectId);
                return Db.Queryable<ec_CaseEntity>().AS(tableName).First(x => x.CaseID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion
        #region 提交数据
        public bool SwitchCase(string ProjId, List<string> Ids, string NewCaseId)
        {
            Repository<ec_enginedataEntity> TagRepo = new Repository<ec_enginedataEntity>();
            var TagTableName = ProjectSugar.TableName<ec_enginedataEntity>(ProjId);
            TagRepo.AsUpdateable().SetColumns(x => x.CaseID == NewCaseId).AS(TagTableName).Where(x => Ids.Contains(x.EngineDataID)).ExecuteCommand();
            return true;
        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            try
            {
                caseServ.DeleteEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, ref string keyValue, ec_CaseEntity entity)
        {
            try
            {
                //需要判断下是否重名 
                string tableName = ProjectSugar.TableName<ec_CaseEntity>(ProjectId);
                var exist = caseRepo.AsQueryable().AS(tableName).First(x => x.CaseName.ToUpper().Trim() == entity.CaseName.ToUpper().Trim());
                if (exist == null)
                {
                    caseServ.SaveEntity(ProjectId, ref keyValue, entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion
    }
}
